//
//  CountDownButton.m
//  CountdownButton
//
//  Created by adiljan on 16/9/2.
//  Copyright © 2016年 adiljan. All rights reserved.
//

#import "CountDownButton.h"
#import "CountdownTool.h"

#define KTITLERADIO 0.2
@implementation CountDownButton

+ (id)buttonWithType:(UIButtonType)buttonType frame:(CGRect)frame title:(NSString *)title image:(UIImage *)image handler:(tapHandler)handler
{
    CountDownButton *button = [super buttonWithType:buttonType];
    button.frame = frame;
    [button setTitle:title forState:UIControlStateNormal];
    button.titleLabel.textAlignment = NSTextAlignmentCenter;
    button.titleLabel.font = [UIFont systemFontOfSize:16];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    button.handler = handler;
    [button addTarget:button action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    // 默认倒计时 60秒
    button.time = 60;
    
    return button;
}

- (void)btnClick:(UIButton *)sender{
    if (self.handler) {
        self.handler(sender);
        
        UIButton *btn = (UIButton *)sender;
        [CountdownTool Countdown:btn startWithTime:10 btnTitle:self.btnTitle];
    }
}

#pragma 调整内部 UIlabel 的frame
- (CGRect)titleRectForContentRect:(CGRect)contentRect{
    CGFloat titleX = 0;
    CGFloat titleHeight = contentRect.size.height;
    CGFloat titleY = 0;
    CGFloat titleWidth = contentRect.size.width;
    return CGRectMake(titleX, titleY, titleWidth, titleHeight);
}

@end
